<extend name="template/base_index" />

<block name="area_header">
	<link rel="stylesheet" type="text/css" media="all" href="__CSS__/wxshop.css">
	<link type="text/css" rel="stylesheet" src="__CDN__/jquery-uploadify/3.2.1/uploadify.css" />
	<script type="text/javascript" src="__CDN__/jquery-uploadify/3.2.1/jquery.uploadify.min.js"></script>
    <link type="text/css" rel="stylesheet" href="__CDN__/select2/3.5.2/select2.css" />
    <script type="text/javascript" src="__CDN__/select2/3.5.2/select2.min.js"></script>
    <script type="text/javascript" src="__CDN__/select2/3.5.2/select2_locale_zh-CN.js"></script>
</block>

<block name="area_body">
	{:W('Menus/topbar')}
	<div class="admin-main container-fluid">
		{:W('Menus/left')}
		<div class="admin-main-content">
			{:W('Menus/breadcrumb')}

			<div class="col-main">
				<div class="main-hd">
					<h2>编辑商品</h2>
				</div>
				<div class="main-bd clearfix" style="padding: 0 10px;margin-top: 0px;">
					<form class="productForm form-horizontal" method="post">
						<input type="hidden" name="id" value="{$vo.id}" />
                        <input type="hidden" name="property" value="" class="property" />
						<input type="hidden" name="main_img" id="main_img" value="{$vo.main_img}" />
						<input type="hidden" name="img" id="img" value="{$vo.img}" />
                        <input type="hidden" name="img_post" id="img_post" value="{$vo.img_post}" />
                        <input type="hidden" name="img_post_bg" id="img_post_bg" value="{$vo.img_post_bg}" />
						<input type="hidden" name="store_id" value="{$vo.store_id}" />
						<h3><i>●</i>基本信息</h3>
                        <div class="js_prop form-group col-lg-12 col-md-12 clearfix">
                            <label for="" class="control-label col-lg-2 col-md-2">商品属性
                                <br/><span class="help-block">(选填)</span>
                            </label>
                            <div class="col-lg-10 col-md-10">
                                <div class="well">
                                    <div class="loading">
                                        <img src="__CDN__/common/loading.gif" />
                                    </div>
                                    <div class="prop-list clearfix">

                                    </div>
                                    <hr>
                                    <div class="btn-controls">
                                        <a href="{:U('Admin/CategoryProp/add',array('cate_id'=>$cate_id))}" target="_blank" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i>添加属性</a>
                                        <a href="javascript:void(0);"  class="btn btn-sm btn-primary js_prop_refresh"><i class="fa fa-refresh"></i>刷新</a>
                                    </div>

                                </div>
                                <span class="help-block">商品属性必须重新编辑，如果保持原样，则都不要选!</span>
                            </div>
                        </div>
						<div class="form-group col-lg-12 col-md-12 clearfix">
							<label for="" class="control-label col-lg-2 col-md-2">商品名称</label>
							<div class="col-lg-10 col-md-10">
								<input name="product_name" maxlength="60" value="{$vo.name}" class="js_product_name form-control input-normal input-sm" />
								<span class="help-block">限30个字</span>
							</div>
						</div>
						
						<div class="form-group col-lg-12 col-md-12 clearfix">
							<label for="" class="control-label col-lg-2 col-md-2">商品图片</label>
							<div class="col-lg-10 col-md-10">
								主图<span class="text-muted">(建议尺寸为640像素*640像素，大小不超过500kb)<i data-toggle="tooltip" class="fa fa-question" title="商品主图将会作为商品的默认图片出现在货架及商品详情页。"></i></span>
								<!-- 图片选择DOM结构 -->
								<div class="wxuploaderimg main_img clearfix <notempty name="vo.main_img">checked</notempty>"  data-maxitems="1">
									<div class="img-preview clearfix" >
										<notempty name="vo.main_img">
											<div class="pull-left clearfix img-item">
												<img src="{:U('Api/Picture/index',array('id'=>$vo['main_img'],'size'=>120))}" data-imageid="{$vo.main_img}"/>
												<div class="edit_pic_wrp"><a href="javascript:;" class="fa fa-lg fa-trash js_delete"></a></div>
											</div>
										</notempty>
									</div>
									<div class="add">
										<i class="fa fa-plus"></i>
									</div>
								</div>
								
								<!-- 图片选择DOM结构 -->
							</div>
						</div>
						
						<div class="form-group col-lg-12 col-md-12 clearfix">
							<label for="" class="control-label col-lg-2 col-md-2">&nbsp;</label>
							<div class="col-lg-10 col-md-10">
								其他图片<span class="text-muted">(选传，单张图片大小不超过500kb，最多10张)<i class="fa fa-question" data-toggle="tooltip" title="将出现在商品图片库，方便用户更好的了解您的商品。"></i></span>
								<!-- 图片选择DOM结构 -->
								<div class="wxuploaderimg clearfix product-imglist "  data-maxitems="10">
									<div class="img-preview clearfix" <if condition="count($imgs) gt 0">style="display:inline-block;"</if>>										
										<if condition="count($imgs) gt 0">
											<volist name="imgs" id="one">
											<div class="pull-left clearfix img-item">
												<img src="{:U('Api/Picture/index',array('id'=>$one,'size'=>120))}" alt="image" data-imageid="{$one}"/>
												<div class="edit_pic_wrp"><a href="javascript:;" class="fa fa-lg fa-trash js_delete"></a></div>
											</div>
											</volist>
										</if>
									</div>
									<div class="add" <if condition="count($imgs) eq 10">style="display:none;"</if> >
										<i class="fa fa-plus"></i>
									</div>
								</div>
								
								<!-- 图片选择DOM结构 -->
							</div>
						</div>




							<div class="form-group col-lg-12 col-md-12 clearfix">
								<label for="" class="control-label col-lg-2 col-md-2">商品海报</label>
								<div class="col-lg-10 col-md-10">
									海报<span class="text-muted">(尺寸比例3:4,大小不要超过500kb)<i data-toggle="tooltip" class="fa fa-question" title="商品海报专为手机端服务。"></i></span>
									<!-- 图片选择DOM结构 -->
									<div class="wxuploaderimg img_post clearfix <neq name="vo.img_post" value="0">checked</neq>"  data-maxitems="1">
									<div class="img-preview clearfix" >
										<neq name="vo.img_post" value="0">
											<div class="pull-left clearfix img-item">
												<img src="{:U('Api/Picture/index',array('id'=>$vo['img_post'],'size'=>120))}" data-imageid="{$vo.img_post}"/>
												<div class="edit_pic_wrp"><a href="javascript:;" class="fa fa-lg fa-trash js_delete"></a></div>
											</div>
											<else/>

										</neq>
									</div>
									<div class="add">
										<i class="fa fa-plus"></i>
									</div>
								</div>

								<!-- 图片选择DOM结构 -->
							</div>
						</div>

                        <div class="form-group col-lg-12 col-md-12 clearfix">
                            <label for="" class="control-label col-lg-2 col-md-2">&nbsp;</label>
                            <div class="col-lg-10 col-md-10">
                                背景<span class="text-muted">(尺寸比例宽高比9:16,适合大小1080*1920,大小不要超过500kb)<i data-toggle="tooltip" class="fa fa-question" title="商品海报主要作为海报的背景。"></i></span>
                                <!-- 图片选择DOM结构 -->
                                <div class="wxuploaderimg clearfix img_post_bg <neq name="vo.img_post_bg" value="0">checked</neq>"  data-maxitems="1">
                                    <div class="img-preview clearfix" >
                                        <neq name="vo.img_post_bg" value="0">
                                            <div class="pull-left clearfix img-item">
                                                <img src="{:U('Api/Picture/index',array('id'=>$vo['img_post_bg'],'size'=>120))}" data-imageid="{$vo.img_post_bg}"/>
                                                <div class="edit_pic_wrp"><a href="javascript:;" class="fa fa-lg fa-trash js_delete"></a></div>
                                            </div>
                                            <else/>

                                        </neq>
                                    </div>
                                    <div class="add">
                                        <i class="fa fa-plus"></i>
                                    </div>
                                </div>
                                <!-- 图片选择DOM结构 -->
                            </div>
                        </div>


    <div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku ">
        <label for="" class="control-label col-lg-2 col-md-2">商品货号<i data-toggle="tooltip" class="fa fa-question" title="跨境填写料号"></i>
            <br/><span class="help-block"></span>
        </label>
        <div class="col-lg-10 col-md-10">
            <input name="product_code" maxlength="60" value="{$vo.product_code}" class="oriprice form-control input-short input-sm" />
            <span class="help-block"></span>
        </div>
    </div>

                        <div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku">
							<label for="" class="control-label col-lg-2 col-md-2">销售价</label>
							<div class="col-lg-10 col-md-10">
								<input name="price" class="price form-control input-short input-sm" value="{:($vo['_price'])}" />元
								<span class="help-block">销售价,需低于原价</span>
							</div>
						</div>
						<div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku">
							<label for="" class="control-label col-lg-2 col-md-2">原价
								<br/><span class="help-block">(选填)</span>
							</label>
							<div class="col-lg-10 col-md-10">
								<input name="ori_price" maxlength="60"  value="{:($vo['ori_price'])}"  class="oriprice form-control input-short input-sm" />元
								<span class="help-block"></span>
							</div>
						</div>
						<div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku hide">
							<label for="" class="control-label col-lg-2 col-md-2 ">商品库存
								<br/><span class="help-block"></span>
							</label>
							<div class="col-lg-10 col-md-10">
								<input name="quantity" maxlength="60"  value="{$vo.quantity}"  class="oriprice form-control input-short input-sm" />
								<span class="help-block"></span>
							</div>
						</div>
                        <div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku">
                            <label for="" class="control-label col-lg-2 col-md-2 ">商品总销量
                                <br/><span class="help-block"></span>
                            </label>
                            <div class="col-lg-10 col-md-10">
                                <input name="total_sales" maxlength="60" value="{$vo.total_sales}" class="form-control input-short input-sm" />
                            </div>
                        </div>

                        <div class="form-group col-lg-12 col-md-12 clearfix ">
                            <label for="" class="control-label col-lg-2 col-md-2 ">计量单位
                                <br/><span class="help-block"></span>
                            </label>
                            <div class="col-lg-10 col-md-10">
                                <select name="dt_goods_unit" style="width: 100px;" class="dt_goods_unit input-normal" >
                                    {:W("Partials/datatree",array(getDatatree('GOODSUNIT'),true,$vo['dt_goods_unit'],1))}
                                </select>
                            </div>
                        </div>

						<div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_sku">
							<label for="" class="control-label col-lg-2 col-md-2 ">商品重量（毛重）
								<br/><span class="help-block"></span>
							</label>
							<div class="col-lg-10 col-md-10">
								<input name="weight" maxlength="60"  value="{$vo.weight}"  class="oriprice form-control input-short input-sm" />克
								<span class="help-block">商品单个重量(毛重)，单位：g（克）</span>
							</div>
						</div>

                        <div class="form-group col-lg-12 col-md-12 clearfix">
                            <label for="" class="control-label col-lg-2 col-md-2">产销国</label>
                            <div class="col-lg-10 col-md-10">

                                <input name="source" maxlength="60" VALUE="{$vo.source}" type="hidden" class="form-control input-normal input-sm" />
                                <select name="dt_origin_country" style="width: 120px;" class="dt_origin_country input-normal" >
                                    {:W("Partials/datatree",array(getDatatree('COUNTRY'),true,$vo['dt_origin_country'],1))}
                                </select>

                                <span class="help-block">商品产销国</span>
                            </div>
                        </div>


						<div class="form-group col-lg-12 col-md-12 clearfix">
							<label for="synopsis" class="col-md-2 col-lg-2 control-label">商品简介</label>
							<div class="col-md-10 col-lg-10">
								<textarea name="synopsis" id="synopsis" class="form-control" rows="5">{$vo.synopsis}</textarea>
								<div class="help-block">(您最多可以输入500个字)</div>
							</div>
						</div>

						
						<div class="form-group col-lg-12 col-md-12 clearfix js_frm_gp_buylimit">
							<label for="" class="control-label col-lg-2 col-md-2">是否限购</label>
							<div class="col-lg-10 col-md-10">
								<label class="radio-inline">
									<input type="radio" name="isbuylimit" <eq name="vo['buy_limit']" value="0">checked="checked"</eq> value="0">不限购
								</label>
								<label class="radio-inline">
									<input type="radio" name="isbuylimit" <neq name="vo['buy_limit']" value="0">checked="checked"</neq> value="1">限购
								</label>

								<input type="text" style="width: 100px;" name="buylimit" value="{$vo.buy_limit}"  class="<eq name="vo['buy_limit']" value="0">hidden</eq> input-sm" />
							</div>
						</div>
										
						<h3 class="pull-left hide"><i>●</i>售后信息</h3>

						<div class="form-group col-lg-12 col-md-12 clearfix hide">
							<label for="" class="control-label col-lg-2 col-md-2">发票</label>
							<div class="col-lg-10 col-md-10">
								<label class="radio-inline">
									<input type="radio" name="has_receipt" <eq name="vo['has_receipt']" value="0">checked="checked"</eq> value="0">无
								</label>
								<label class="radio-inline">
									<input type="radio" name="has_receipt" <eq name="vo['has_receipt']" value="1">checked="checked"</eq> value="1">有
								</label>

							</div>
						</div>
						
						<div class="form-group col-lg-12 col-md-12 clearfix hide">
							<label for="" class="control-label col-lg-2 col-md-2">保修</label>
							<div class="col-lg-10 col-md-10">
								<label class="radio-inline">
									<input type="radio" name="under_guaranty" <eq name="vo['under_guaranty']" value="0">checked="checked"</eq>  checked="checked" value="0">无
								</label>
								<label class="radio-inline">
									<input type="radio" name="under_guaranty" <eq name="vo['under_guaranty']" value="1">checked="checked"</eq>  value="1">有
								</label>

							</div>
						</div>
						
						<div class="form-group col-lg-12 col-md-12 clearfix hide">
							<label for="" class="control-label col-lg-2 col-md-2">退换货</label>
							<div class="col-lg-10 col-md-10">
								<label class="radio-inline">
									<input type="radio" name="support_replace" <eq name="vo['support_replace']" value="0">checked="checked"</eq>   value="0">不支持
								</label>
								<label class="radio-inline">
									<input type="radio" name="support_replace" <eq name="vo['support_replace']" value="1">checked="checked"</eq>   value="1">支持
								</label>

							</div>

						</div>

						<div class="form-group col-lg-12 col-md-12 clearfix tool-bar text-center border">
							<a target-form="productForm" href="{:U('Admin/Product/edit')}" onclick="return check();"  class="ajax-post btn btn-primary" id="js_submit"><i class="fa fa-check"></i>确定</a>
							<a href="{:U('Admin/Product/index',array('store_id'=>$vo['store_id'],'onshelf'=>$onshelf,'p'=>$p))}" class="btn btn-default"><i class="fa fa-reply"></i>返回</a>
						</div>
					</form>
				</div>
			</div>

			<include file="template/wxpicture" />
		</div>
		<!-- END admin-main-content -->
	</div>
	<!-- END admin-main-->
</block>

<block name="area_footer">
	<script type="text/javascript" ></script>
	
	
	<script type="text/javascript">
		window.ServicesURL = {
			groupGetAll:"{:U('Admin/WxshopGroup/groups')}",	
			groupAdd:"{:U('Admin/WxshopGroup/add')}",	
			cateAllProp:"{:U('Admin/Product/cateAllProp')}",
			skulist:"{:U('Admin/Product/skulist')}",
		};
	</script>
	<script type="text/javascript">
        //国别选择时的操作
        function initCountry(){
            $(".dt_origin_country").change(function(){
                var val = $(".dt_origin_country").find("option:selected").text();
                $("input[name='source']").val(val);
            });
            $(".dt_origin_country").change();
        }

        //初始化select2 控件
        function initSelect2(){
            $('select').select2();
        }

        function getData(){

            $(".property").val("");
            $(".propsel").each(function(index,item){
                $propvalue = $(item).val();
                if($propvalue != "=请选择="){
                    $(".property").val($(".property").val()+$propvalue+";");
                }
            });
			
			var main_img = $(".main_img .img-preview img").attr("data-imageid");
			if(main_img){
				$("#main_img").val(main_img);
			}

			$("#img").val("");
			$(".product-imglist .img-preview img").each(function(index,item){
				$("#img").val($("#img").val()+$(item).attr("data-imageid")+",");
				//alert($("#img").val());
			});


            var img_post = $(".img_post .img-preview img").attr("data-imageid");
            if(img_post){
                $("#img_post").val(img_post);
            }

            var img_post_bg = $(".img_post_bg .img-preview img").attr("data-imageid");
            if(img_post_bg){
                $("#img_post_bg").val(img_post_bg);
            }
			//alert($("#img").val());
		}
		
		
		function check(){
			getData();
			var txt = $(".js_product_name").val();
			var len = txt.replace(/[^\x00-\xff]/g, 'xx').length ; 
			if(len == 0){					
				$.scojs_message('商品名称必须填写', $.scojs_message.TYPE_ERROR);
				return false;
			}
			if(len > 30){
				$.scojs_message('商品名称不能超过30个字', $.scojs_message.TYPE_ERROR);
				return false;
			}
			return true;
		}

        function appendProp(list){

            $ele = $(".js_prop .prop-list");
            $ele.empty();
            for(var i=0;i<list.length;i++){
                //创建一个dropdown
                $propName = $('<span>'+list[i].name+'</span>');
                $prop = $('<div class="dropdown dropdown-toggle"></div>');
                $ele.append($("<div class='prop-item' ></div>").attr("data-id",list[i].id).append($propName).append($prop));

                if(list[i].property_value){

                    $propValue = $("<select class='propsel propvalue-list dropdown-menu'></select>");
                    $prop.append($propValue);
                    var propvaluelist = list[i].property_value;
                    $propValue.append("<option class='propvalue-item'>=请选择=</option>");
                    for(var j=0;j<propvaluelist.length;j++){
                        $propValueItem = $("<option class='propvalue-item'></option>");
                        $propValueItem.attr("data-id",propvaluelist[j].id).text(propvaluelist[j].valuename).val(list[i].id+","+propvaluelist[j].id);
                        $propValue.append($propValueItem);
                    }
                }

            }


        }



        //商品属性
        function queryProp() {


            $(".js_prop .loading").show();
            var last_cate = "{$cate_id}";
            $.post(window.ServicesURL.cateAllProp, {
                cate_id: last_cate
            }).done(function(data) {
                //
                if (data.status) {
                    if(data.info == null){
                        $ele = $(".js_prop .prop-list");
                        $ele.html("无属性");
                        return ;
                    }
                    appendProp(data.info);
                    $(".propsel").select2({
                        placeholder: "=选择=",
                        language: "zh-CN",
                    });
                } else {
                    $.scojs_message('添加失败!', $.scojs_message.TYPE_ERROR);
                }
            }).always(function(){
                $(".js_prop .loading").hide();
            });
        }

        function productProp(){

            $(".js_prop .prop-list").click(function(ev){
                $ele = $(ev.target);
                if(!$ele.hasClass('link_a')){
                    return ;
                }
                $propitem = $ele.parents(".prop-item");
                $ele.addClass("selected");
                $propitem.find("button.dropdown-toggle").html($ele.text()+"<span class='caret'></span>");
            });

            setTimeout(queryProp,700);
        }
	
		$(function() {
            productProp();
			initCountry();
            initSelect2();
			//图片上传
			wxuploadimg.init({cont:".wxuploaderimg"});
			//
			$(".js_frm_gp_buylimit input[type=radio]").click(function(){
				console.log($(this).val());
				console.log($(".js_frm_gp_buylimit input[type=text]"));
				if($(this).val() == 1){
					$(".js_frm_gp_buylimit input[type=text]").removeClass("hidden");
				}else{
					$(".js_frm_gp_buylimit input[type=text]").addClass("hidden");
				}
			});
			
			
		})
		
	</script>
	
</block>